package com.gamesnet.ihm;

import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.gamesnet.api.Api;
import com.gamesnet.models.Jeu;
import com.gamesnet.models.Listable;
import com.gamesnet.util.AdapterListener;
import com.gamesnet.util.MessageDialog;
import com.gamesnet.widget.ListAdapter;

public class JeuxActivity extends Activity implements AdapterListener{
	
	long userId;

	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.jeux);

		Bundle b = getIntent().getExtras();
		userId = b.getLong("userId");
		
		String id = String.valueOf(userId);

		try {
			List<Jeu> jeux = Api.jeux(id);
			ListAdapter<Jeu> adapter = new ListAdapter<Jeu>(this, jeux);
			adapter.addListener(this);
			adapter.setUserId(userId);
			ListView list = (ListView)findViewById(R.id.listJeux);
			list.setAdapter(adapter);


		} catch (JSONException ex) {
			ex.printStackTrace();
			Log.e("API CONSOLES", ex.getMessage() );
		}

	}

	public void onClickText(Listable model, int position) {
		Intent intent = new Intent(this, InfoGameActivity.class);
		intent.putExtra("gameId", model.getId());
		startActivity(intent);
	}

	public void onClickSupp(Listable model, int position) {
		MessageDialog.showMessage("supprimer"+model.toString(), this);	

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, "Voir son mur");
		return true; 
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) { 
		case 1:
			Log.v("compte", "clique le menu item");
			Intent intent = new Intent(this, WallActivity.class);
			intent.putExtra("userId", userId);
			startActivity(intent);
			return true;
		default:
			return true;
		} 
	}


}
